﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using StartupSolution.ViewModel;

namespace StartupSolution.Views.Maps
{
    public partial class GeoImageryBinding : Page
    {
        private GeoLocationViewModel _geoLocationViewModel;

        public GeoImageryBinding()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _geoLocationViewModel = new GeoLocationViewModel();

            //Handle the xamMap Loaded event
            theMap.Loaded += new RoutedEventHandler(theMap_Loaded);
        }

        void theMap_Loaded(object sender, RoutedEventArgs e)
        {
            SetDefaultLocation();

            //TODO: Uncomment the following method to automatically navigate to a point of interest.
            //GoToPointOfInterest();
        }

        private void SetDefaultLocation()
        {
            // Convert Geodetic to Cartesian coordinates
            Point winTopLeft = theMap.MapProjection.ProjectToMap(new Point(-180, 90));
            Point winBottomRight = theMap.MapProjection.ProjectToMap(new Point(180, -90));
            theMap.WindowRect = this.theMap.WorldRect = new Rect(new Point(0, 0), new Point(0, 0));

            // Create Rect structure the map control's WindowRect and WorldRect
            Rect winRect = new Rect();
            winRect.X = Math.Min(winTopLeft.X, winBottomRight.X);
            winRect.Y = Math.Min(winTopLeft.Y, winBottomRight.Y);
            winRect.Width = Math.Abs(winTopLeft.X - winBottomRight.X);
            winRect.Height = Math.Abs(winTopLeft.Y - winBottomRight.Y);

            // Change the map control's WindowRect and WorldRect
            theMap.WindowRect = theMap.WorldRect = winRect;
        }

        private void GoToPointOfInterest()
        {
            //TODO: Add cutom logic for point of interest selection.

            //Point of interest top left and bottom right points:
            Point worldTopLeft = new Point(_geoLocationViewModel.GeoLocations[0].Longitude - 2, _geoLocationViewModel.GeoLocations[0].Latitude + 2);
            Point worldBottomRight = new Point(_geoLocationViewModel.GeoLocations[0].Longitude + 2, _geoLocationViewModel.GeoLocations[0].Latitude - 2);

            // Convert Geodetic to Cartesian coordinates
            Point winTopLeft = theMap.MapProjection.ProjectToMap(worldTopLeft);
            Point winBottomRight = theMap.MapProjection.ProjectToMap(worldBottomRight);
            theMap.WindowRect = new Rect(new Point(0, 0), new Point(0, 0));

            // Create Rect structure the map control's WindowRect and WorldRect
            Rect winRect = new Rect();
            winRect.X = Math.Min(winTopLeft.X, winBottomRight.X);
            winRect.Y = Math.Min(winTopLeft.Y, winBottomRight.Y);
            winRect.Width = Math.Abs(winTopLeft.X - winBottomRight.X);
            winRect.Height = Math.Abs(winTopLeft.Y - winBottomRight.Y);

            // Change the map control's WindowRect and WorldRect
            theMap.WindowRect = winRect;
        }
    }
}
